package com.wocao.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.SysUser;
import com.wocao.service.SysUserService;
import com.wocao.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;

/**
 * OSS文件上传
 */
@CrossOrigin
@RestController
public class UploadController {

    @Autowired
    private SysUserService userService;

    @PostMapping("/upload/{username}")
    public ResponseResult<String> upload(@PathVariable String username, MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        OssUtils.upload(file.getInputStream(), filename);
        String url = OssUtils.getURL(filename);
        String substring = url.substring(0, url.indexOf("?"));
        userService.update(new UpdateWrapper<SysUser>().set("avatar", substring).eq("user_name", username));
        return ResponseResult.ok(substring);
    }







}
